﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAC.sqlHelper;
using SAC.Elink;
using SAC.Helper;
using System.Collections;
using System.Data;
using SAC.Power;

namespace DAL
{
    public class DALBwjc
    {
        string sql = "";
        string errMsg = "";

        StrHelper sh = new StrHelper();
        DateHelper dh = new DateHelper();

        public ArrayList retBwjcByDate(string qsrq, string jsrq, string unit)
        {
            double val = 0;

            string str = "";
            string desc = "";       //故障类型表 拼接字符串
            string value = "";      //拼接值字符串
            string[] zb = null;
            string[] data = null;
            string[] monVal = new string[6];
            string[] monCon = new string[6];

            ArrayList list = new ArrayList();

            if (unit == "0")
                sql = "select * from 超温考核测点表 where 类别='BW15'";
            else
                sql = "select * from 超温考核测点表 where 机组=" + unit + " and 类型='BW15'";

            DataTable dtCeDian = DBsql.RunDataTable(sql, out errMsg);

            if (dtCeDian != null && dtCeDian.Rows.Count > 0)
            {
                zb = new string[dtCeDian.Rows.Count];
                data = new string[dtCeDian.Rows.Count];

                for (int i = 0; i < dtCeDian.Rows.Count; i++)
                {
                    sql = "select * from 超温考核故障表 where 考核点ID='" + dtCeDian.Rows[i]["考核点ID"].ToString() + "' order by 故障类型ID";

                    DataTable dtGZ = DBsql.RunDataTable(sql, out errMsg);

                    if (dtGZ != null && dtGZ.Rows.Count > 0)
                    {
                        string strRes = "";
                        for (int a = 0; a < dtGZ.Rows.Count - 1; a++)
                        {
                            string gzID = dtGZ.Rows[a]["故障类型ID"].ToString();

                            if (gzID == "GRQH1")
                                str = "585-587";
                            else if (gzID == "ZRQH1")
                                str = "603-605";
                            else if (gzID == "GRQH2")
                                str = ">587";
                            else if (gzID == "ZRQH2")
                                str = ">605";
                            else if (gzID == "GRQH3")
                                str = ">590";
                            else if (gzID == "ZRQH3")
                                str = ">608";


                            desc = dtCeDian.Rows[i]["考核点描述"].ToString();

                            sql = "select * from 超温考核记录表 where 开始时间>='" + qsrq + "' and  结束时间<='" + jsrq + "' and 故障类型ID='" + gzID + "' and 考核点ID='";
                            sql += dtCeDian.Rows[i]["考核点ID"].ToString() + "'";

                            DataTable dtJL = DBsql.RunDataTable(sql, out errMsg);

                            if (dtJL != null && dtJL.Rows.Count > 0)
                            {
                                for (int j = 0; j < dtJL.Rows.Count; j++)
                                {
                                    if (dtJL.Rows[j]["开始时间"].ToString() != "" && dtJL.Rows[j]["结束时间"].ToString() != "")
                                    {
                                        DataRow drQS = DBsql.RunDataRow("select * from 排班表 where '" + dtJL.Rows[j]["开始时间"] + "' between 起始时间 and 结束时间", out errMsg);
                                        DataRow drJS = DBsql.RunDataRow("select * from 排班表 where '" + dtJL.Rows[j]["结束时间"] + "' between 起始时间 and 结束时间", out errMsg);

                                        if (drQS["班组编号"].ToString() == drJS["班组编号"].ToString())
                                        {
                                            //计算15分钟
                                            if (gzID == "GRQH1" || gzID == "ZRQH1")
                                            {
                                                val = dh.GetSecOfDateDiff(DateTime.Parse(dtJL.Rows[j]["结束时间"].ToString()), DateTime.Parse(dtJL.Rows[j]["开始时间"].ToString()));

                                                if (val > 15 * 60)
                                                {
                                                    val = val - 15 * 60;

                                                    monVal[int.Parse(drQS["班组编号"].ToString())] += val.ToString() + "+";
                                                    monCon[int.Parse(drQS["班组编号"].ToString())] += "1+";
                                                }
                                            }
                                            else
                                            {
                                                val = dh.GetSecOfDateDiff(DateTime.Parse(dtJL.Rows[j]["结束时间"].ToString()), DateTime.Parse(dtJL.Rows[j]["开始时间"].ToString()));

                                                monVal[int.Parse(drQS["班组编号"].ToString())] += val.ToString() + "+";
                                                monCon[int.Parse(drQS["班组编号"].ToString())] += "1+";
                                            }
                                        }
                                        else
                                        {
                                            //跨班组
                                            DataTable dtBz = DBsql.RunDataTable("select * from 排班表 where 起始时间 between '" + dtJL.Rows[j]["开始时间"].ToString() + "' and '" + dtJL.Rows[j]["结束时间"].ToString() + "' union select * from 排班表 where 结束时间 between '" + dtJL.Rows[j]["开始时间"].ToString() + "' and '" + dtJL.Rows[j]["结束时间"].ToString() + "'", out errMsg);
                                            if (dtBz.Rows.Count > 0)
                                            {
                                                string qsTime = "";
                                                string jsTime = "";

                                                for (int k = 0; k < dtBz.Rows.Count; k++)
                                                {
                                                    qsTime = dtBz.Rows[k]["起始时间"].ToString();
                                                    jsTime = dtBz.Rows[k]["结束时间"].ToString();

                                                    if (DateTime.Parse(dtJL.Rows[j]["开始时间"].ToString()) > DateTime.Parse(qsTime) && DateTime.Parse(dtJL.Rows[j]["开始时间"].ToString()) < DateTime.Parse(jsTime))
                                                    {
                                                        val = dh.GetSecOfDateDiff(DateTime.Parse(jsTime), DateTime.Parse(dtJL.Rows[j]["开始时间"].ToString()));
                                                    }
                                                    else if (DateTime.Parse(dtJL.Rows[j]["结束时间"].ToString()) > DateTime.Parse(qsTime) && DateTime.Parse(dtJL.Rows[j]["结束时间"].ToString()) < DateTime.Parse(jsTime))
                                                    {
                                                        val = dh.GetSecOfDateDiff(DateTime.Parse(dtJL.Rows[j]["结束时间"].ToString()), DateTime.Parse(qsTime));
                                                    }
                                                    else
                                                    { val = dh.GetSecOfDateDiff(DateTime.Parse(jsTime), DateTime.Parse(qsTime)); }


                                                    if (gzID == "GRQH1" || gzID == "ZRQH1")
                                                    {
                                                        if (val > 15 * 60)
                                                        {
                                                            val = val - 15 * 60;

                                                            monVal[int.Parse(dtBz.Rows[k]["班组编号"].ToString())] += val.ToString() + "+";
                                                            monCon[int.Parse(dtBz.Rows[k]["班组编号"].ToString())] += "1+";
                                                        }
                                                    }
                                                    else
                                                    {
                                                        monVal[int.Parse(dtBz.Rows[k]["班组编号"].ToString())] += val.ToString() + "+";
                                                        monCon[int.Parse(dtBz.Rows[k]["班组编号"].ToString())] += "1+";
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                for (int j = 1; j <= 5; j++)
                                {
                                    monVal[j] = "0+";
                                    monCon[j] = "0+";
                                }
                            }

                            //计算次数喝值
                            zb[i] = desc;//

                            value = "";
                            for (int l = 1; l <= 5; l++)
                            {
                                string res = "0";
                                string res1 = "0";
                                string mon = "0";
                                string mon1 = "0";

                                if (monVal[l] != null)
                                    mon = monVal[l].TrimEnd('+');

                                if (monCon[l] != null)
                                    mon1 = monCon[l].TrimEnd('+');

                                try
                                {
                                    res = StrHelper.Cale("(" + mon + ")/60");
                                    res = Math.Ceiling(double.Parse(res)).ToString();

                                    res1 = StrHelper.Cale(mon1);
                                }
                                catch { }

                                value += res + "," + res1 + ";";

                                monCon[l] = "";
                                monVal[l] = "";
                            }

                            strRes += str + "+" + value + "&";
                        }
                        data[i] = strRes;
                        strRes = "";
                    }
                }
            }

            list.Add(zb);
            list.Add(data);

            return list;
        }

        public ArrayList retBwjcByDateByFD(string qsrq, string jsrq, string unit)
        {
            double val = 0;

            string str = "";
            string desc = "";       //故障类型表 拼接字符串
            string value = "";      //拼接值字符串
            string[] zb = null;
            string[] data = null;
            string[] monVal = new string[6];
            string[] monCon = new string[6];

            ArrayList list = new ArrayList();

            if (unit == "0")
                sql = "select * from 超温考核测点表 where 类别='BWFD'";
            else
                sql = "select * from 超温考核测点表 where 机组=" + unit + " and 类型='BWFD'";

            DataTable dtCeDian = DBsql.RunDataTable(sql, out errMsg);

            if (dtCeDian != null && dtCeDian.Rows.Count > 0)
            {
                zb = new string[dtCeDian.Rows.Count];
                data = new string[dtCeDian.Rows.Count];

                for (int i = 0; i < dtCeDian.Rows.Count; i++)
                {
                    sql = "select * from 超温考核故障表 where 考核点ID='" + dtCeDian.Rows[i]["考核点ID"].ToString() + "' order by 故障类型ID";

                    DataTable dtGZ = DBsql.RunDataTable(sql, out errMsg);

                    if (dtGZ != null && dtGZ.Rows.Count-1 > 0)
                    {
                        string strRes = "";
                        for (int a = 0; a < dtGZ.Rows.Count; a++)
                        {
                            string gzID = dtGZ.Rows[a]["故障类型ID"].ToString();

                            if (gzID == "GRQFH1")
                                str = "587-590";
                            else if (gzID == "ZRQFH1")
                                str = "605-608";
                            else if (gzID == "GRQFH2")
                                str = "590-593";
                            else if (gzID == "ZRQFH2")
                                str = "608-611";
                            else if (gzID == "GRQFH3")
                                str = ">=593";
                            else if (gzID == "ZRQFH3")
                                str = ">=611";
                            else if (gzID == "GRQFL2")
                                str = "560-570";
                            else if (gzID == "GRQFL1")
                                str = "<=560";
                            else if (gzID == "ZRQFL2")
                                str = "575-585";
                            else if (gzID == "ZRQFL1")
                                str = "<=575";

                            desc = dtCeDian.Rows[i]["考核点描述"].ToString();

                            sql = "select * from 超温考核记录表 where 开始时间>='" + qsrq + "' and  结束时间<='" + jsrq + "' and 故障类型ID='" + gzID + "' and 考核点ID='";
                            sql += dtCeDian.Rows[i]["考核点ID"].ToString() + "'";

                            DataTable dtJL = DBsql.RunDataTable(sql, out errMsg);

                            if (dtJL != null && dtJL.Rows.Count > 0)
                            {
                                for (int j = 0; j < dtJL.Rows.Count; j++)
                                {
                                    if (dtJL.Rows[j]["开始时间"].ToString() != "" && dtJL.Rows[j]["结束时间"].ToString() != "")
                                    {
                                        DataRow drQS = DBsql.RunDataRow("select * from 排班表 where '" + dtJL.Rows[j]["开始时间"] + "' between 起始时间 and 结束时间", out errMsg);
                                        DataRow drJS = DBsql.RunDataRow("select * from 排班表 where '" + dtJL.Rows[j]["结束时间"] + "' between 起始时间 and 结束时间", out errMsg);

                                        if (drQS["班组编号"].ToString() == drJS["班组编号"].ToString())
                                        {
                                            val = dh.GetSecOfDateDiff(DateTime.Parse(dtJL.Rows[j]["结束时间"].ToString()), DateTime.Parse(dtJL.Rows[j]["开始时间"].ToString()));

                                            monVal[int.Parse(drQS["班组编号"].ToString())] += val.ToString() + "+";
                                            monCon[int.Parse(drQS["班组编号"].ToString())] += "1+";
                                        }
                                        else
                                        {
                                            //跨班组
                                            DataTable dtBz = DBsql.RunDataTable("select * from 排班表 where 起始时间 between '" + dtJL.Rows[j]["开始时间"].ToString() + "' and '" + dtJL.Rows[j]["结束时间"].ToString() + "' union select * from 排班表 where 结束时间 between '" + dtJL.Rows[j]["开始时间"].ToString() + "' and '" + dtJL.Rows[j]["结束时间"].ToString() + "'", out errMsg);
                                            if (dtBz.Rows.Count > 0)
                                            {
                                                string qsTime = "";
                                                string jsTime = "";

                                                for (int k = 0; k < dtBz.Rows.Count; k++)
                                                {
                                                    qsTime = dtBz.Rows[k]["起始时间"].ToString();
                                                    jsTime = dtBz.Rows[k]["结束时间"].ToString();

                                                    if (DateTime.Parse(dtJL.Rows[j]["开始时间"].ToString()) > DateTime.Parse(qsTime) && DateTime.Parse(dtJL.Rows[j]["开始时间"].ToString()) < DateTime.Parse(jsTime))
                                                    {
                                                        val = dh.GetSecOfDateDiff(DateTime.Parse(jsTime), DateTime.Parse(dtJL.Rows[j]["开始时间"].ToString()));
                                                    }
                                                    else if (DateTime.Parse(dtJL.Rows[j]["结束时间"].ToString()) > DateTime.Parse(qsTime) && DateTime.Parse(dtJL.Rows[j]["结束时间"].ToString()) < DateTime.Parse(jsTime))
                                                    {
                                                        val = dh.GetSecOfDateDiff(DateTime.Parse(dtJL.Rows[j]["结束时间"].ToString()), DateTime.Parse(qsTime));
                                                    }
                                                    else
                                                    { val = dh.GetSecOfDateDiff(DateTime.Parse(jsTime), DateTime.Parse(qsTime)); }

                                                    monVal[int.Parse(dtBz.Rows[k]["班组编号"].ToString())] += val.ToString() + "+";
                                                    monCon[int.Parse(dtBz.Rows[k]["班组编号"].ToString())] += "1+";

                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                for (int j = 1; j <= 5; j++)
                                {
                                    monVal[j] = "0+";
                                    monCon[j] = "0+";
                                }
                            }

                            //计算次数和值
                            zb[i] = desc;//

                            value = "";
                            for (int l = 1; l <= 5; l++)
                            {
                                string res = "0";
                                string res1 = "0";
                                string mon = "0";
                                string mon1 = "0";

                                if (monVal[l] != null)
                                    mon = monVal[l].TrimEnd('+');

                                if (monCon[l] != null)
                                    mon1 = monCon[l].TrimEnd('+');

                                try
                                {
                                    res = StrHelper.Cale("(" + mon + ")/60");
                                    res = Math.Ceiling(double.Parse(res)).ToString();

                                    res1 = StrHelper.Cale(mon1);
                                }
                                catch { }

                                value += res + "," + res1 + ";";

                                monCon[l] = "";
                                monVal[l] = "";
                            }

                            strRes += str + "+" + value + "&";
                        }
                        data[i] = strRes;
                        strRes = "";
                    }
                }
            }

            list.Add(zb);
            list.Add(data);

            return list;
        }
    }
}
